﻿using MobiSms.DataModel;
using MobiSms.Enums;
using MobiSms.Library;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
using MobiSms.Library.Helpers;
using MobiSms.Library.Extensions;

namespace MobiSms.ViewModel
{
    public class SerialRegisterViewModel : MainViewModel
    {
        public SerialRegisterViewModel()
        {
            SoapClient.RegisterCompleted -= SoapClient_RegisterCompleted;
            SoapClient.RegisterCompleted += SoapClient_RegisterCompleted;
        }

        void SoapClient_RegisterCompleted(object sender, RegisterCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.code == 0)
                {
                    Database.CollectionOfSerialNumbers.InsertOnSubmit(SerialItemToAdd);
                    Submit();
                    MessageBox.Show(Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_DONE));
                    NavigateBack();
                }
                else
                {
                    ShowResult<ResultCodeValue>(e.Result.code);
                    //MessageBox.Show(((ResultCodeValue)e.Result.code).ToString());
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }

        private SerialInfo _serialItemToAdd = new SerialInfo();
        public SerialInfo SerialItemToAdd
        {
            get { return _serialItemToAdd; }
            set
            {
                _serialItemToAdd = value;
                RaisePropertyChanged(() => SerialItemToAdd);
            }
        }

        private string _selectedProvince;
        public string SelectedProvince
        {
            get { return _selectedProvince; }
            set
            {
                _selectedProvince = value;
                RaisePropertyChanged(() => SelectedProvince);
                RaisePropertyChanged(() => Cities);
            }
        }

        private XDocument XmlDocument()
        {
            Stream stream = Application.GetResourceStream(new System.Uri("/MobiSms.Wasun;Component/ChinaArea.xml", System.UriKind.RelativeOrAbsolute)).Stream;
            XDocument xdocument = XDocument.Load(stream);
            stream.Close();
            return xdocument;
        }

        public IEnumerable<string> Provinces
        {
            get
            {
                return (from element in XmlDocument().Descendants("province")
                        select element.Attribute("province").Value);
            }
        }

        public IEnumerable<string> Cities
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(SelectedProvince))
                {
                    IEnumerable<XElement> elements = XmlDocument().Descendants("province");
                    int count = elements.Count();
                    XElement element = elements.FirstOrDefault(e => e.Attribute("province").Value == SelectedProvince);
                    return (from ele in element.Descendants("City")
                            select ele.Attribute("City").Value);
                }
                return null;
            }
        }

        public ICommand RegisterCommand
        {
            get {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        SoapClient.RegisterAsync(SerialItemToAdd.SerialNumber,
                            SerialItemToAdd.Password,
                            SerialItemToAdd.Province,
                            SerialItemToAdd.City,
                            SerialItemToAdd.Address,
                            SerialItemToAdd.Fullname,
                            SerialItemToAdd.Linkman,
                            SerialItemToAdd.Phone,
                            SerialItemToAdd.Mobile,
                            SerialItemToAdd.Email,
                            SerialItemToAdd.Signature);
                    }
                };
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return true; },
                    ExecuteCallback = delegate
                    {
                        NavigateBack();
                    }
                };
            }
        }
    }
}
